<?php

namespace App\Http\Controllers;

use App\Facades\Product;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Psy\Util\Json;

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware('benchmark:test1,test2');
    }

    public function index()
    {
        return 'hello laravel';
    }

    public function dbtest()
    {
        $ret = DB::table('users')->where('id', 3)
            ->increment('score', 5);
        dd($ret);
    }

    public function modelTest()
    {
//        $product =Product::query()->create([
//            'title'       => '水杯',
//            'category_id' => 1,
//            'is_on_sale'  => 1,
//            'price'       => '1200',
//            'attr'        => ['高' => '10cm', '容积' => '200ml']
//        ]);

//        $product = Product::query()->insert([
//            'title' => '水杯2',
//            'category_id' => 1,
//            'is_on_sale' => 1,
//            'price' => '1200',
//            'attr' => json_encode(['高' => '10cm', '容积' => '200ml'])
//        ]);
//        $product = new Product();
//        $product->fill([
//            'title' => '水杯1',
//            'category_id' => 1,
//            'is_on_sale' => 1,
//            'price' => '1200',
//            'attr' => json_encode(['高' => '10cm', '容积' => '200ml'])
//        ]);
//        $product->id = '水杯4';
//        $product->save();
//        $product = Product::all();
//        $product = Product::query()->get();
//        $product = Product::query()->where('is_on_sale',1)->get();

//        $product = Product::query()->where('id',1)->update(['is_on_sale'=> 0]);
//        $product = Product::query()->find(1);
//        $product->title = '保温杯';
//        $product->save();
//        $product = Product::query()->where('id',2)->delete();
//        $product = Product::withTrashed()->find(2);
//        $product->restore();
//        dd($product);
    }

    public function testCollection()
    {
//        $collect = collect([1,2,3]);
//        dd($collect->toArray());
//        $collect = collect(['k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3']);
//        $keys = $collect->keys()->toArray();
//        $values = $collect->values()->toArray();
//        dd($keys,$values);
//        dd($collect->last());
//        dd($collect->only(['k1','k2']));

        $product = Product::all();
//        $product->pluck('title')->dump();
//        $product->take(2)->dump();
//        $ret = $product->pluck('title')->implode(',');

//        $ret = $product->pluck('title')->toArray();
//        $ret = implode(',',$ret);

//        聚合运算
//        $product = Product::all()->pluck('price');
//        $count = $product->count();
//        $sum = $product->sum();
//        $average = $product->average();
//        $max = $product->max();
//        $min = $product->min();
//        dd($max);

//        查找判断
//        $exists = collect(['v1','v2','v3'])->contains('v2');
//        dd($exists);
//        collect([1, 2, 3])->diff([2, 3])->dd();
//        $collect = collect(['k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3']);
//        $is = $collect->has('k4');
//        dd($is);
//        $collect = collect([]);
//        dd($collect->isEmpty());
//        $product = Product::all();
//        $pro = $product->where('id',3);
//        dd($pro);

//        遍历
        $product = Product::all();
//        $product->each(function ($item) {
//            var_dump($item->id);
//        });
//        $ret = $product->map(function ($item) {
//            return $item->id;
//        });
//        dd($product, $ret->toArray());
//        $keyBy = $product->keyBy('id')->toArray();
//        dd($product->toArray(),$keyBy);
//        $group = $product->groupBy('category_id');
//        dd($group->toArray());
//        $product->filter(function ($item) {
//            return $item->id > 3;
//        })->dd();
//        $collect = collect(['k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3']);
//        dd($collect->flip()->toArray());
//        dd($collect->reverse()->toArray());
//        collect([12, 21, 5, 6, 8, 7])->sort()->dd();
//        collect([12, 21, 5, 6, 8, 7])->sortDesc()->dd();

//        collect(['k1','k2'])->combine(['v1','v2'])->dd();
//        collect(['k1','k2'])->crossJoin(['v1','v2'])->dd();
    }

    public function cacheTest()
    {
//        Cache::put('key1', 'value1', '10');
//        Cache::put('key2', 'value2');
//        Cache::put('key3', 'value3', now()->addMinutes(1));
//
//        $v1 = Cache::get('key1', 'default1');
//        $v2 = Cache::get('key2', 'default2');
//        $v3 = Cache::get('key3', 'default3');
//
//        $is = Cache::add('key4','value2','20');
//        Cache::forever('key5','value5');
//
//        Cache::forget('key2');
//        Cache::put('key4','','0');
//
//        Cache::increment('key6',1);
//        Cache::increment('key6',2);
//        Cache::decrement('key6',1);
//
//        $key5=Cache::pull('key5');
//        dd($key5);
//        dd(Cache::get('key6'));
//
//        dd($v1, $v2, $v3);

    }

    public function facadesTest()
    {
        Product::getProduct(12);
        Product::getProduct(22);
    }
}
